package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacCultureMedium;
import com.ysd.lis.entity.lab.LabItemDict;
import com.ysd.lis.mapper.bac.BacCultureMediumMapper;
import com.ysd.lis.mapper.lab.LabItemDictMapper;
import com.ysd.lis.request.BacCultureMediumDto;
import com.ysd.lis.service.bac.BacCultureMediumService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author bai
 * @since 2024-04-10
 */
@Service
public class BacCultureMediumServiceImpl extends ServiceImpl<BacCultureMediumMapper, BacCultureMedium> implements BacCultureMediumService {
    @Autowired
    BacCultureMediumMapper bacCultureMediumMapper;
    @Autowired
    LabItemDictMapper labItemDictMapper;

    @Override
    public List<BacCultureMedium> findBacCultureMediumList(BacCultureMedium bacCultureMedium) {
        MPJLambdaWrapper<BacCultureMedium> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacCultureMedium.class);
        queryWrapper.selectAs("itemDict", LabItemDict::getItemNa, BacCultureMedium::getItemNa);
        if (!StringUtils.isEmpty(bacCultureMedium.getSearchValue())) {
            queryWrapper.and(wrapper -> wrapper.like(BacCultureMedium::getCode, bacCultureMedium.getSearchValue()).or().like(BacCultureMedium::getName, bacCultureMedium.getSearchValue()).or().like(BacCultureMedium::getInputCode, bacCultureMedium.getSearchValue()));
        }
        queryWrapper.leftJoin(LabItemDict.class, "itemDict", p -> p.isNotNull(BacCultureMedium::getRptGroup).isNotNull(BacCultureMedium::getItemNo).eq(LabItemDict::getRptGroup, BacCultureMedium::getRptGroup).eq(LabItemDict::getItemNo, BacCultureMedium::getItemNo));
        List<BacCultureMedium> bacCultureMedia = bacCultureMediumMapper.selectList(queryWrapper);
        /*bacCultureMedia.forEach(a->{
            LabItemDict labItemDict = labItemDictMapper.selectById(a.getProjectId());
            a.setProjectName(labItemDict==null?"":labItemDict.getItemNa());
        });*/
        return bacCultureMedia;
    }

    @Override
    public Result addBacCultureMedium(BacCultureMedium bacCultureMedium) {
        return Result.succ(bacCultureMediumMapper.insert(bacCultureMedium));
    }

    @Override
    public Result deleteBacCultureMedium(BacCultureMediumDto bacCultureMediumDto) {
        List<BacCultureMedium> bacCultureMedia = bacCultureMediumDto.getBacCultureMedia();

        List<String> ids = bacCultureMedia.stream().map(BacCultureMedium::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result updateBacCultureMedium(BacCultureMedium bacCultureMedium) {
        return Result.succ(bacCultureMediumMapper.updateById(bacCultureMedium));
    }

    @Override
    public Result findBacCultureMediumListByIds(List<String> ids) {
        List<BacCultureMedium> bacCultureMediumList = bacCultureMediumMapper.selectBatchIds(ids);
        return Result.fail(1, "查询成功", bacCultureMediumList);
    }

    @Override
    public BacCultureMedium findFirstItemByNo(String no) {
        if (StringUtils.isNotBlank(no)) {
            MPJLambdaWrapper<BacCultureMedium> queryWrapper = new MPJLambdaWrapper<>();
            queryWrapper.eq(BacCultureMedium::getDelFlag, 0);
            queryWrapper.eq(BacCultureMedium::getCode, no);
            queryWrapper.orderByAsc(BacCultureMedium::getOrderNum);
            List<BacCultureMedium> lst = bacCultureMediumMapper.selectList(queryWrapper);
            if (lst.isEmpty()) {
                return null;
            }
            return lst.get(0);
        }
        return null;
    }

    @Override
    public Result updateBatchBacCultureMedium(BacCultureMediumDto bacCultureMediumDto) {
        List<BacCultureMedium> bacCultureMedia = bacCultureMediumDto.getBacCultureMedia();
        boolean b = this.saveOrUpdateBatch(bacCultureMedia);
        if (b) {
            return Result.succ(1, "批量更新成功", null);
        } else {
            return Result.fail(400, "批量更新失败", "");
        }
    }

    @Override
    public Object findBacCultureMediums(BacCultureMedium bacCultureMedium) {
        MPJLambdaWrapper<BacCultureMedium> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacCultureMedium.class);
        queryWrapper.selectAs("itemDict", LabItemDict::getItemNa, BacCultureMedium::getItemNa);
        if (!StringUtils.isEmpty(bacCultureMedium.getSearchValue())) {
            queryWrapper.and(wrapper -> wrapper.like(BacCultureMedium::getCode, bacCultureMedium.getSearchValue()).or().like(BacCultureMedium::getName, bacCultureMedium.getSearchValue()).or().like(BacCultureMedium::getInputCode, bacCultureMedium.getSearchValue()));
        }
        queryWrapper.eq(BacCultureMedium::getDelFlag, 0);
        queryWrapper.and(q->q.ne(BacCultureMedium::getMediumType,"2") .or().isNull(BacCultureMedium::getMediumType));
        queryWrapper.leftJoin(LabItemDict.class, "itemDict", p -> p.isNotNull(BacCultureMedium::getRptGroup).isNotNull(BacCultureMedium::getItemNo).eq(LabItemDict::getRptGroup, BacCultureMedium::getRptGroup).eq(LabItemDict::getItemNo, BacCultureMedium::getItemNo));
        List<BacCultureMedium> bacCultureMedia = bacCultureMediumMapper.selectList(queryWrapper);
        /*bacCultureMedia.forEach(a->{
            LabItemDict labItemDict = labItemDictMapper.selectById(a.getProjectId());
            a.setProjectName(labItemDict==null?"":labItemDict.getItemNa());
        });*/
        return bacCultureMedia;
    }
}
